/**   
 * 项目名：phms3
 * 日期：    2019-12-03 22:37:20  
 * Copyright (c) 2015- HTSC-版权所有     
 */

package com.joysuch.wwyt.bp.repository;

import com.joysuch.wwyt.bp.entity.BpProductionFacilityType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * BpProductionFacilityTypeDao
 * 
 */
public interface BpProductionFacilityTypeDao extends JpaRepository<BpProductionFacilityType, Long> {

    public Page<BpProductionFacilityType> findAll(Specification<BpProductionFacilityType> spec, Pageable pageable);

    public BpProductionFacilityType save(BpProductionFacilityType bean);

    public void delete(BpProductionFacilityType bean);

    public BpProductionFacilityType findByName(String name);

    @Query("select t.name from BpProductionFacilityType t where t.id=:id")
    String findNameById(Long id);

    @Modifying
    @Query("update BpProductionFacilityType set number=(select count(m) from BpProductionFacility m where type=?1) where id=?1")
    void updateDeviceCount(Long typeId);

    @Query("select id from BpProductionFacilityType where name = ?1")
    Long findIdByName(String type);

    @Query("select count(m) from BpProductionFacilityType m where m.parentId = ?1")
    public Long countByParentId(Long id);

    @Query(value = "select * from wwyt_bp_production_facility_type where org_code like :companyCode%  and delete_flag='0' ",nativeQuery = true)
    public List<BpProductionFacilityType> findList(String companyCode);

    int countByName(String name);
}
